package com.lanhe.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.product.domain.LhHaokaProduct;
import com.lanhe.product.domain.LhHaokaProductDetails;
import com.lanhe.product.mapper.LhHaokaProductMapper;
import com.lanhe.product.service.ILhHaokaProductDetailsService;
import com.lanhe.product.service.ILhHaokaProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 号卡产品表(LhHaokaProduct)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-25 20:19:29
 */
@Service
public class LhHaokaProductServiceImpl extends ServiceImpl<LhHaokaProductMapper, LhHaokaProduct> implements ILhHaokaProductService {

	@Autowired
	private LhHaokaProductMapper productMapper;


	@Autowired
	private ILhHaokaProductDetailsService contentService;

	/**
	 * 分页查询
	 *
	 * @param product 实体
	 * @return java.util.List<com.lanhe.product.domain.LhKayuProduct>
	 * @author zhanggl
	 * @date 2023/12/25
	 */
	@Override
	public List<LhHaokaProduct> selectHaokaProductList(LhHaokaProduct product) {
		LambdaQueryWrapper<LhHaokaProduct> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtils.isNotBlank(product.getProductName()), LhHaokaProduct::getProductName, product.getProductName());
		queryWrapper.ge(ObjectUtil.isNotNull(product.getBeginTime()), LhHaokaProduct::getCreateTime, product.getBeginTime());
		queryWrapper.le(ObjectUtil.isNotNull(product.getEndTime()), LhHaokaProduct::getCreateTime, product.getEndTime());
		queryWrapper.eq(ObjectUtil.isNotNull(product.getType()), LhHaokaProduct::getType, product.getType());
		queryWrapper.eq(LhHaokaProduct::getDelFlag, Constants.GeneralIntType.ZERO);
		queryWrapper.orderByDesc(LhHaokaProduct::getProductId);
		return productMapper.selectList(queryWrapper);
	}

	/**
	 * 查询详情
	 *
	 * @param productId 产品ID
	 * @return com.lanhe.product.domain.LhKayuProduct
	 * @author zhanggl
	 * @date 2023/12/25
	 */
	@Override
	public LhHaokaProduct selectHaokaProductById(Long productId) {
		LambdaQueryWrapper<LhHaokaProduct> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhHaokaProduct::getDelFlag, 0)
				.eq(LhHaokaProduct::getProductId, productId);
		LhHaokaProduct haokaProduct = productMapper.selectOne(queryWrapper);
		LhHaokaProductDetails productContent = contentService.selectHaokaProductContentByProductId(productId);
		if (ObjectUtil.isNotNull(productContent)) {
			haokaProduct.setContent(productContent.getContent());
		}
		return haokaProduct;
	}

	/**
	 * 插入
	 *
	 * @param product 实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/25
	 */
	@Override
	public int insertHaokaProduct(LhHaokaProduct product) {
		int insertNum = productMapper.insert(product);
		contentService.insertHaokaProductContent(product.getProductId(),product.getContent());
		return insertNum;
	}

	/**
	 * 修改
	 *
	 * @param product 实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/25
	 */
	@Override
	public int updateHaokaProduct(LhHaokaProduct product) {
		LambdaQueryWrapper<LhHaokaProduct> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhHaokaProduct::getDelFlag, 0)
				.eq(LhHaokaProduct::getProductId, product.getProductId());
		int updateNum = productMapper.update(product, queryWrapper);
		contentService.updateHaokaProductContent(product.getProductId(),product.getContent());
		return updateNum;
	}

	/**
	 * 删除
	 *
	 * @param productIds 产品IDS
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/25
	 */
	@Override
	public int deleteHaokaProductByIds(Long[] productIds) {
		LambdaUpdateWrapper<LhHaokaProduct> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(LhHaokaProduct::getDelFlag, Constants.GeneralIntType.TWO+"")
				.in(LhHaokaProduct::getProductId,productIds);
		return productMapper.update(null,updateWrapper);
	}
}

